2. The Cacheable and CacheFlush Annotations

The Springcache plugin provides two annotations that are the basis of how you can apply caching and flushing behaviour to both Spring bean methods and page fragments. Both annotations are in the grails.plugin.springcache.annotations package.

The @Cachable annotation

The Cacheable annotation is applied to methods on Spring managed beans such as Grails services to cache method results or to controller actions to cache page fragments. The annotation requires a cache name specified either as a standalone value or with the cache element. The following declarations are equivalent:

@Cacheable("myCache")
@Cacheable(cache = "myCache")

To resolve the cache name in a non-standard way you can supply a cacheResolver element, see Cache Selection. For content caching only you can also supply a keyGenerator element, see Content Cache Keys

The @CacheFlush annotation

The CacheFlush annotation can be applied in the same places as the Cacheable annotation but instead of caching results it will cause a cache or set of caches to be flushed. The CacheFlush annotation requires a single element or a String array either as a standalone value or with the caches element. Either way the elements can simply be literal cache names or regular expression patterns that may match multiple cache names. For example:

@CacheFlush("myCache")
@CacheFlush(/w+ControllerCache/)
@CacheFlush(["cacheA", "cacheB", "cacheC"])
@CacheFlush(caches = ["cacheA", "cacheB", "cacheC"])
@CacheFlush([/cache[A-Z]/, "myCache"])

To resolve the cache names in a non-standard way you can supply a cacheResolver element, see Cache Selection.